//
//  LcUIUtility.m
//  18Birdies
//
//  Created by Lc on 15/3/31.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcUIUtility.h"
#import "UIView+Lc.h"
#import "UIDevice+Lc.h"

@implementation LcUIUtility

+ (CGFloat)screenWidth {
  return [UIScreen mainScreen].bounds.size.width;
}

+ (CGFloat)screenHeight {
  return [UIScreen mainScreen].bounds.size.height;
}

+ (CGFloat)screenCenterX {
  return self.screenWidth / 2;
}

+ (CGFloat)screenCenterY {
  return self.screenHeight / 2;
}

+ (CGFloat)statusBarHeight {
  return [UIApplication sharedApplication].statusBarFrame.size.height;
}

+ (CGFloat)navigationBarHeight {
  return 44;
}

+ (CGFloat)topSafeAreaHeight {
  if (@available(iOS 11.0, *)) {
    if ([UIDevice isPortrait]) {
      UIWindow* window = [[UIApplication sharedApplication] windows].firstObject;
      CGRect safeFrame = window.safeAreaLayoutGuide.layoutFrame;
      return CGRectGetMinY(safeFrame);
    }
  }
  return 0;
}

+ (CGFloat)bottomSafeAreaHeight {
  if (@available(iOS 11.0, *)) {
    if ([UIDevice isPortrait]) {
      UIWindow* window = [[UIApplication sharedApplication] windows].firstObject;
      CGRect safeFrame = window.safeAreaLayoutGuide.layoutFrame;
      return CGRectGetMaxY(window.frame) - CGRectGetMaxY(safeFrame);
    }
  }
  return 0;
}

+ (CGFloat)leftSafeAreaWidth {
  if (@available(iOS 11.0, *)) {
    if (![UIDevice isPortrait]) {
      UIWindow* window = [[UIApplication sharedApplication] windows].firstObject;
      CGRect safeFrame = window.safeAreaLayoutGuide.layoutFrame;
      return CGRectGetMinX(safeFrame);
    }
  }
  return 0;
}

+ (CGFloat)rightSafeAreaWidth {
  if (@available(iOS 11.0, *)) {
    if (![UIDevice isPortrait]) {
      UIWindow* window = [[UIApplication sharedApplication] windows].firstObject;
      CGRect safeFrame = window.safeAreaLayoutGuide.layoutFrame;
      return CGRectGetMaxX(window.frame) - CGRectGetMaxX(safeFrame);
    }
  }
  return 0;
}

+ (CGFloat)contentHeight {
  return [self screenHeight] - [self topGuideY];
}

+ (CGFloat)topGuideY {
  return self.statusBarHeight + self.navigationBarHeight;
}

+ (CGFloat)navigationBarLeft {
  if (@available(iOS 13.0, *)) {
    return 0;
  } else {
    if ([self screenWidth] >= 414) {
      return 20;
    }
    return 16;
  }
}

+ (UIColor*)debugBackgroundColor {
  CGFloat red = arc4random_uniform(128) / 255.0f + 0.5f;
  CGFloat green = arc4random_uniform(128) / 255.0f + 0.5f;
  CGFloat blue = arc4random_uniform(128) / 255.0f + 0.5f;
  return [UIColor colorWithRed:red green:green blue:blue alpha:0.3];
}

+ (CGFloat)pointsOfPixels:(CGFloat)pixels {
  return pixels / [UIScreen mainScreen].scale;
}

// size calculate
+ (CGSize)size:(CGSize)size addEdge:(UIEdgeInsets)edge {
  return CGSizeMake(size.width + edge.left + edge.right, size.height + edge.top + edge.bottom);
}

+ (CGSize)size:(CGSize)size minusEdge:(UIEdgeInsets)edge {
  return CGSizeMake(size.width - edge.left - edge.right, size.height - edge.top - edge.bottom);
}
@end
